import Vue from 'vue'
import Vuex from 'vuex'
import { loginAPI } from '../api/login'

Vue.use(Vuex)

export default new Vuex.Store({
  strict: true,
  state: {
    token: window.localStorage.getItem('token') || '',
    refreshToken: window.localStorage.getItem('refresh_token') || ''
  },
  mutations: {
    setToken (state, tokenStr) {
      state.token = tokenStr
      window.localStorage.setItem('token', tokenStr)
    },
    setRefreshToken (state, refreshTokenStr) {
      state.refreshToken = refreshTokenStr
      window.localStorage.setItem('refreshToken', refreshTokenStr)
    }
  },
  actions: {
    async asyncLoginAction (store, formLogin) {
      // 发送请求
      const resp = await loginAPI(formLogin)
      // 这个地方的data忘记删除一个，导致进不去
      const { token, refresh_token: refreshToken } = resp.data
      store.commit('setToken', token)
      store.commit('setRefreshToken', refreshToken)
    }
  },
  modules: {
  }
})
